Skip to main content

⚑ How a GitHub Actions Runner Works (Step by Step)

GitHub Actions runners execute workflows when triggered by events like code pushes, pull requests, or scheduled tasks. Here’s a step-by-step breakdown of how it works:


πŸš€ 1. Triggering the Pipeline​

  • You push or pull code to GitHub (or trigger any defined event). πŸ”„
  • GitHub detects this event and assigns the job to an available runner (either GitHub-hosted or self-hosted). πŸ–₯️

πŸ“Œ Example Triggers:

  • push to main branch πŸ“€
  • Opening a pull request πŸ”„
  • A cron job running at scheduled times ⏰

πŸ“₯ 2. Runner Fetches the Code​

  • The runner starts on a machine (either a GitHub-provided VM or your own self-hosted server). πŸ—οΈ
  • It clones (downloads) the latest code from your GitHub repository. πŸ“‚
  • The runner sets up the working environment as defined in the YAML workflow file.

πŸ› οΈ 3. Runner Executes the Steps​

  • It follows the instructions defined in the GitHub Actions YAML file (.github/workflows/your-workflow.yml).
  • Each step is executed in the specified order.

πŸ“Œ Example Steps: βœ… Set up Node.js πŸ”§
βœ… Install dependencies (e.g., npm install) πŸ“¦
βœ… Run tests (e.g., npm test) πŸ§ͺ
βœ… Build the project (e.g., npm run build) πŸ—οΈ
βœ… Lint and format code (e.g., eslint . --fix) 🎨


πŸ“‘ 4. Deploying to Another Server (Optional Step)​

  • The runner itself does not run your application; it only executes tasks like testing, building, and deploying.
  • If your YAML file includes an SSH step, the runner will:
    • πŸ”‘ Use SSH to connect to the production server.
    • πŸ“€ Copy the built files to the production server using rsync or scp.
    • πŸ”„ Restart services (e.g., pm2 restart app, docker-compose up -d).

🎯 Key Takeaways​

βœ… GitHub Actions automates testing, building, and deployment processes.
βœ… Runners execute jobs based on YAML workflow instructions.
βœ… Self-hosted runners provide more control, while GitHub-hosted runners are managed automatically.
βœ… Deployments often involve SSH & file transfers to production environments.

πŸ’‘ With GitHub Actions, you can automate your entire CI/CD pipeline effortlessly! πŸš€πŸ”₯


πŸ“œ GitHub Actions CI/CD Pipeline with Deployment

πŸ› οΈ Prerequisites:​

Before running the pipeline, you must store your server’s private key securely in GitHub:

  1. Go to your GitHub Repository β†’ Settings β†’ Actions β†’ Secrets and Variables.
  2. Add a new secret with the name DOCUMENT_UBUNTU_SERVER.

πŸ“Œ GitHub Actions Workflow YAML​

name: GitHub Action push-based CI/CD Pipeline

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest ## Replace with self-hosted runner if needed

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Use Node.js 22
uses: actions/setup-node@v3
with:
node-version: 22

- name: Install dependencies
run: npm install -f

- name: Build project
run: npm run build

- name: Setup SSH Key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.DOCUMENT_UBUNTU_SERVER }}" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H 13.201.191.75 >> ~/.ssh/known_hosts

- name: Deploy build files to EC2
run: |
rsync -avz --delete -e "ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no" ./build/ root@13.201.191.75:/var/www/html/my-docs/build/

πŸ” Explanation of Each Step:​

  1. Checkout Code: Pulls the latest code from GitHub.
  2. Set Up Node.js: Uses version 22 for the project.
  3. Install Dependencies: Ensures all required packages are installed.
  4. Build Project: Runs the build command.
  5. Setup SSH Key:
    • Stores the private key securely.
    • Adds the remote server to known_hosts to prevent SSH confirmation prompts.
  6. Deploy to EC2:
    • Uses rsync to copy the build files securely to the production server.

πŸš€ Final Thoughts​

βœ… Automates deployment with every push to main.
βœ… Secures SSH access with GitHub Secrets .
βœ… Ensures seamless deployments by syncing only necessary files.

πŸ’‘ With this setup, you achieve a fully automated, secure, and efficient CI/CD pipeline! πŸ”₯πŸ”₯